<template>
  <div>
    权限分配
    <!-- :check-strictly  取消父子组件的关联 -->
    <el-tree
      ref="tree"
      :data="list"
      :props="{label: 'name'}"
      show-checkbox
      check-strictly
      default-expand-all
      node-key="id"
    />
    <el-row slot="footer" type="flex" justify="center">
      <el-col :span="6">
        <el-button size="small">取消</el-button>
        <el-button
          size="small"
          type="primary"
          @click="hSubmit"
        >确定</el-button>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { getPermissionList } from '@/api/permisson'
import { arrayToTree } from '@/utils/index'
import { getRoleDetail, assignPerm } from '@/api/setting'
export default {
  props: {
    roleId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      list: [] // 存储权限数据
    }
  },
  created() {
    // 调用接口，获取所有的权限点数据
    this.loadPermission()
    // // 调用接口,获取当前这个id所有的权限点数据
    // this.loadPermissionById()
  },
  methods: {
    async hSubmit() {
      // 1. 当前用户选中的节点的 id
      const permIds = this.$refs.tree.getCheckedKeys()
      console.log('permIds', permIds)
      // 2. 调用api
      await assignPerm({
        id: this.roleId,
        // permIds: permIds
        permIds
      })
      // 3. 关闭弹框
      this.$emit('success')
    },
    // 获取所有的权限列表
    async loadPermission() {
      const res = await getPermissionList()
      this.list = arrayToTree(res.data)
    },
    // 获取角色现有的权限
    async loadPermissionById() {
      const res = await getRoleDetail(this.roleId)
      console.log('当前id', res)
      // 当前角色的权限是： res.data.permIds
      // 回填
      this.$refs.tree.setCheckedKeys(res.data.permIds)
    }
  }
}
</script>

<style>

</style>
